
"""/****************************************************************************
Title:    Slave device to control 2 Qi chargers
Author:   Kartik Karuna <kartik.karuna@exeger.com>,
File:     $Id: TOP_pad.py ,v 1.2 2013/10/1 17:07:18 Kartik Exp $
Software: synapse IDE
Hardware: SM700 , Ver 2.4.33

Description:
    this code reads 
Include Files (Libs)
     synapse.nvparams
     synapse.platforms 
*****************************************************************************/"""

#Lib
from synapse.platforms import *
from synapse.nvparams import *
from synapse.hexSupport import *
#from string import *


#Global variables
ms = 30
sec_counter=0
knock2=0
coil=0
searching_state=False 
ldr_va1=0
ldr_va2=0

##inputs
#ADC
KNOCK = 9
LDR_A = 10
LDR_B = 11

#status
#led1 = 28                     # Knock pickup DEBUG
charge_indicator_G_A=35       #check this or 39
Coil_trouble_A =5                #MISO

charge_indicator_G_B=36          #change to A6
Coil_trouble_B =29            #KB7 
##Output 
#LEDs
Green_led_A=26         #LED Ring On Coil A , G_coil_led_g_A (KB4)
Red_led_A=4           #G_coil_led_R_A (SS)

Green_led_B=10         #LED Ring On Coil B  #G_coil_led_G_B (TMR2)
Red_led_B=27          #G_coil_led_R_B (KB5)
#Fuel Cell Control
F_cell_A =7          #SCK
F_cell_b =11         #TMR3
#Mosfets                   # Coil Control
charge_on_A=8             #TMR0v 
charge_on_B=9              #TMR1
##I2C add
INA219_A=0x0
INA219_B=0x0
Fuel_CELL=0x0


"""#Check Platform While compiling 
if platform != "SM700":
    compileError                    #script only valid on SM700
"""

#function to broadcast received data to portal or master
def sendData(mdata,Id):
     serverAddr = '\x00\x00\x01'           # hard-coded address for Portal (PC)
     rpc(serverAddr,"logEvent",mdata,Id)
     
def sendLDR(mdata,Id):
     serverAddr = '\x00\x00\x01'           # hard-coded address for Portal (PC)
     rpc(serverAddr,"logLDR",mdata,Id)

def makeInput(pin):
    setPinDir(pin, False)          # set direction of the pin as input
    setPinPullup(pin, True)
    #monitorPin(pin, True)          # Monitor for button press
    
def makeOutput(pin):
    setPinDir(pin, True)          # set direction of the pin as output
    writePin(pin, False)
#
#
#  Turn coil on and search for phone    
def searching(state):
    global searching_state 
    searching_state = state
    if state==True:
        monitorPin(charge_indicator_G_A, True)
        monitorPin(charge_indicator_G_B, True)
    if coil==1:                                    #check if coil 1 is already charging then only ring 2 is turned on 
        writePin(charge_on_B,state) 
        writePin(Green_led_B,state)
    elif coil==2:                                  #check if coil 2 is already charging then only ring 1 is turned on 
        writePin(charge_on_A,state) 
        writePin(Green_led_A,state)
    elif coil==3:                                  # both coils charging , do nothing 
        a=0
    else:
        writePin(charge_on_A,state)                   # Turn both the rings on 
        writePin(Green_led_A,state)
        writePin(charge_on_B,state)                   # Turn both the rings on 
        writePin(Green_led_B,state)

   
    
#Found Cell
def cel_found(port):
   global coil
   
  
    
# Check for Knock   
def readPad_low():
   if (ldr_va1  < 10) :        #230 Treshhold value for Pizo
      return True
   elif ldr_va2 < 10:
       return True                      
   return False  


# Things to do at startup
@setHook(HOOK_STARTUP)
def startupEvent():
    #LEDs
   

    
    makeOutput(charge_on_A)  #coil A
    makeOutput(Green_led_A)
    makeOutput(Red_led_A)
    
    makeOutput(charge_on_B) #Coil B
    makeOutput(Green_led_B)
    makeOutput(Red_led_B)
    
    makeOutput(F_cell_A)
    makeOutput(F_cell_b)
    
    makeInput(charge_indicator_G_A)
    makeInput(charge_indicator_G_B)
    
    makeInput(Coil_trouble_A)
    makeInput(Coil_trouble_B)
    
    #i2cInit(False)
    setRate(2)  
 
 
# Do every 1 S  
@setHook(HOOK_1S)
def timer1SEvent(currentMs):
    global sec_counter

    if searching_state== True:
        sec_counter+=1
        if sec_counter==15:
            sec_counter=0
            #do LIPO READ
            searching(False)  #think this through 

    sendLDR(ldr_va1,'sp18')
    
    sendLDR(ldr_va2,'sp19')
    sendData(coil,'sp3')
    
"""    
# Do every 100 MS  
@setHook(HOOK_100MS)
def timer100MSEvent(currentMs):
   # writePin(led1,False) 
    ##D0 i2c every 500ms
"""    
    
      
# Do every 10 MS  
@setHook(HOOK_10MS)
def timer10MSEvent(currentMs):
    global led1,ms,knock2,ldr_va1,ldr_va2
    sens = 'knock'
    writePin(charge_on_B, True)
    writePin(charge_on_A, True)
     
    
    if ms > 10:
       
      if readPad_low() == True:
          #writePin(led1,True)
          knock2+=1
          
          if ms < 80:
            if knock2 == 2:
             sens=sens + str(ms)
             searching(True)
             sendData(sens,3)
             knock2=0
             ms=0
          else :
            monitorPin(charge_indicator_G_A, False)
            monitorPin(charge_indicator_G_B, False)
            knock2=0
            ms=0           
          
    ms+=1
    
    if ms%2==0:
      ldr_va1=readAdc(LDR_A)
    else:
      ldr_va2=readAdc(LDR_B)
    
    if ms == 500:
       ms = 11
      
""" Do every 1 MS  
@setHook(HOOK_1MS)
def timer1MSEvent(currentMs):
"""   
    
# Do every time there is a change in any monitored PIN     
@setHook(HOOK_GPIN)
# cell found on coil 1
def buttonEvent(pinNum, isSet):
    global coil,searching_state
    
    
    
    if (pinNum == charge_indicator_G_A):
        if isSet == True:
            #searching_state=True
            #writePin(Green_led_A,True)
            writePin(Red_led_A,False)
            if coil==3:
               coil=2
            else:
               coil=0
            #searching(True)
            
        
        else:
            #searching_state=False
            writePin(Green_led_A,False)  # Remove
            writePin(Red_led_A,True)
            if coil==2:                        # raise appropriate flag to indicate charging 
               coil=3
            else:
               coil=1
            
    elif (pinNum == charge_indicator_G_B):
        if isSet == True:
            #searching_state=True
            #writePin(Green_led_B,True)  #Remove
            writePin(Red_led_B,False)
            
            if coil==3:
                coil=1
            elif coil==2 :
                coil=0
            
        else:
            #searching_state=False
            writePin(Green_led_B,False)
            writePin(Red_led_B,True)
            if coil==1:                     # raise appropriate flag to indicate charging 
                coil=3
            else:
                coil=2
    
    
    
    '''    
            
            
    else:
        
"""  
@setHook(HOOK_GPIN)
# cell found on coil 2
def buttonEvent(charge_indicator2, True):
    writePin(led1,True)
    global coil
    if charge_indicator1==True:
        
        writePin(Red_led2,False)
        writePin(Green_led2,True)
        if coil==1:                     # raise appropriate flag to indicate charging 
           coil=3
        else:
           coil=2
    else:
        if coil==3:
            coil=1
        else:
            coil=0
        searching(True)
        
       
     
    """
'''